package com.ceba.test.modules.log.service;

import com.ceba.base.helper.LoggerHelper;
import com.ceba.cebautils.DateUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.enums.LogOpTypeEnum;
import com.ceba.test.base.enums.LogTypeEnum;
import com.ceba.base.web.service.IBaseService;
import com.ceba.test.modules.log.dao.OpLogDao;
import com.ceba.test.modules.log.entity.OpLog;

import abc.IContextDictionary;
import abc.ID;
import abc.data.DataTable;
import abc.data.Sql;
import abc.web.HttpContext;

import java.util.Map;


public class OpLogService extends IBaseService{

	static Logger logger = LoggerFactory.getLogger(OpLogService.class);
	private OpLogDao opLogDao;

	/**
	 * 获取日志列表
	 * 
	 */
	public Sql getLogsList(String txtStartTime,String  txtFinishTime,String txtLogsType,String txtKey, String orderBy) throws Exception {
		return opLogDao.getLogsList(txtStartTime,txtFinishTime,txtLogsType,txtKey,orderBy);
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getLogsListJson(JSONObject o) throws Exception {
		JSONArray arrI = new JSONArray();
		if (o.has("rows")) {
			arrI = o.getJSONArray("rows");
			if (arrI.length() > 0) {
				for (int i = 0; i < arrI.length(); i++) {
					JSONObject jo = arrI.getJSONObject(i);
					if (jo.has("OP_TYPE")) {
						int position = jo.getInt("OP_TYPE");
						if (LogOpTypeEnum.CREATE_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.CREATE_OPYPE.getValue());
						} else if (LogOpTypeEnum.DEL_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.DEL_OPYPE.getValue());
						} else if (LogOpTypeEnum.SEACH_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.SEACH_OPYPE.getValue());
						} else if (LogOpTypeEnum.UPDATE_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.UPDATE_OPYPE.getValue());
						} else if (LogOpTypeEnum.INIT_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.INIT_OPYPE.getValue());
						} else if (LogOpTypeEnum.UPLOAD_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.UPLOAD_OPYPE.getValue());
						} else if (LogOpTypeEnum.CLOSE_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.CLOSE_OPYPE.getValue());
						}else if (LogOpTypeEnum.TASKOPEN_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.TASKOPEN_OPYPE.getValue());
						}else if (LogOpTypeEnum.TASKCLOSE_OPYPE.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.TASKCLOSE_OPYPE.getValue());
						}else if (LogOpTypeEnum.SIGN_NO_NEED.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.SIGN_NO_NEED.getValue());
						}else if (LogOpTypeEnum.SIGN_NEED.getType() == position) {
							jo.put("OP_TYPE", LogOpTypeEnum.SIGN_NEED.getValue());
						}
					}
					
					if (jo.has("LOG_TYPE")) {
						int position = jo.getInt("LOG_TYPE");
						if (LogTypeEnum.REPORT_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.REPORT_LOGTYPE.getValue());
						} else if (LogTypeEnum.INTERFACE_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.INTERFACE_LOGTYPE.getValue());
						} else if (LogTypeEnum.SIGN_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.SIGN_LOGTYPE.getValue());
						} else if (LogTypeEnum.YP_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.YP_LOGTYPE.getValue());
						} else if (LogTypeEnum.MOBILE_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.MOBILE_LOGTYPE.getValue());
						} else if (LogTypeEnum.TASK_LOGTYPE.getType() == position) {
							jo.put("LOG_TYPE", LogTypeEnum.TASK_LOGTYPE.getValue());
						} 
					}

				}
			}
			o.remove("rows");
			o.put("rows", arrI);

		}
		return o.toString();
	}

	// 添加一条日志
	public void addLog(int logType, int opType, String message, String errorMessage,String hashMessage) {
		try {
			OpLog opLog = new OpLog();
			opLog.setId(ID.getGuid());
			opLog.setLogType(logType);
			opLog.setOpType(opType);
			opLog.setMessage(message);
			opLog.setErrorMesage(errorMessage);
			opLog.setHashMessage(hashMessage);
			opLog.setCreateTime(DateUtil.getNowTime(DateUtil.YYYY_MM_DD_HH_MM_SS));
			opLogDao.add(opLog);
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
		}
	}

	/**
	 * 删除一条数据
	 * 
	 * @param id
	 *            数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception {

		return opLogDao.delete(id);
	}

	/**
	 * @return
	 */
	public DataTable getOpLog(String id) throws Exception {

		return opLogDao.getOpLog(id);
	}

	/**
	 * 获取所有不用签名文件个数
	 * 
	 * @return
	 */
	public long getAllNoSignFileCount() throws Exception {
		return opLogDao.getAllNoSignFileCount();
	}
	
	
	public String  getPublicStatic(String txtStartTime,String txtFinishTime,String txtLogsType,  String txtKey,String orderBy) throws Exception{
		JSONObject dt=	opLogDao.getPublicStatic(txtStartTime, txtFinishTime, txtLogsType, txtKey, orderBy).toJSONObject();
		return getLogsListJson(dt);
	}
	
	public JSONObject getLogsList(Map<String,Object> dic) throws Exception {
		return opLogDao.getLogsList(dic);
	}
	
	
	
	public OpLogDao getOpLogDao() {
		return opLogDao;
	}

	public void setOpLogDao(OpLogDao opLogDao) {
		this.opLogDao = opLogDao;
	}
	
}
